package com.cao.meet.util;

import android.annotation.SuppressLint;
import android.app.Service;
import android.app.ZysjSystemManager;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.cao.meet.bean.UpdateTimeBean;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 描述：用于修改系统设置
 * ===============================
 * @author wpfse wpfsean@126.com
 * @Create at:2018/12/25 10:00
 * @version V1.0
 */
public class UpdateSystemSettingService extends Service {

    private @SuppressLint("WrongConstant")
    ZysjSystemManager manager;

    @SuppressLint("WrongConstant")
    @Override
    public void onCreate() {
        super.onCreate();
        manager = (ZysjSystemManager)getSystemService("zysj");
        initialize();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * 初始化
     */
    private void initialize() {

        //修改系统亮度
        updateSystemBrighness();

        //隐藏系统的导航栏
        hideSystembar();

        //修改系统时间
        updateSystemTime();
    }

    //定义get_brighness_time，用来记录获取屏幕背光的次数
    private int get_brighness_time = 0;

    /**
     * 修改系统亮度
     */
    private void updateSystemBrighness() {
        //获取当前屏幕背光亮度
        int result = manager.ZYgetBackLight();
        if (result != -1){
            if (result < 255){
                int set_result = manager.ZYsetBackLight(245);
                if (set_result == 0){
                    ToastUtils.showShort("设置背光成功");
                }else {
                    ToastUtils.showShort("背光设置失败，请重新设置");
                }
            }
        }else {
            //获取屏幕背光失败，重新获取
            if (get_brighness_time < 5){
                updateSystemBrighness();
                get_brighness_time ++;
            }else {
                ToastUtils.showShort("背光获取失败，请联系管理员");
            }
        }
    }

    //定义get_systembar_time，用来记录获取导航栏状态的次数
    private int get_systembar_time = 0;

    /**
     * 隐藏系统的导航栏
     */
    private void hideSystembar() {
        //获取当前导航栏的状态（0.显示状态，1.隐藏状态，-1获取失败）
        int get_state = manager.ZYSystemBar(2);
        if (get_state == 0){
            //当前状态为显示状态，设置导航栏状态为隐藏
            int set_state = manager.ZYSystemBar(1);
            if (set_state == 0){
                ToastUtils.showShort("导航栏状态设置为隐藏");
            }else {
                ToastUtils.showShort("设置导航栏状态失败");
            }
        }else if (get_state == 1){
            //当前状态为隐藏，不用做任何操作
        }else if (get_state == -1){
            if (get_systembar_time < 5){
                hideSystembar();
            }
        }
    }

    /**
     * 修改系统时间
     */
    private void updateSystemTime() {
        /**
         *   思路
         *
         */
        String get_time_url = "http://19.0.0.20/zkth/upDateTime.php";
        String[] system_times = get_system_time();
        final String date = system_times[0];
        String[] hour_minute = system_times[1].split(":");
        final int hour = Integer.parseInt(hour_minute[0]);
        final int minute = Integer.parseInt(hour_minute[1]);
        //创建OKHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        //创建Request对象
        Request mRequest = new Request.Builder().url(get_time_url).build();
        okHttpClient.newCall(mRequest).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String time_results = response.body().string();
                if (!TextUtils.isEmpty(time_results)){
                    UpdateTimeBean timeBean = new Gson().fromJson(time_results, UpdateTimeBean.class);
                    String[] times = timeBean.getTime().split(":");
                    int net_hour = Integer.parseInt(times[0]);
                    int net_minute = Integer.parseInt(times[1]);
                    if (!timeBean.getDate().equals(date)){
                        int time_result = manager.ZYsetSysTime(timeBean.getDate(),timeBean.getTime());
                        if (time_result == 0){
                            ToastUtils.showShort("系统时间设置成功");
                        }else {
                            ToastUtils.showShort("系统时间设置失败");
                        }
                    }else {
                        if (net_hour != hour){
                            int time_result = manager.ZYsetSysTime(timeBean.getDate(),timeBean.getTime());
                            if (time_result == 0){
                                ToastUtils.showShort("系统时间设置成功");
                            }else {
                                ToastUtils.showShort("系统时间设置失败");
                            }
                        }else {
                            if (minute-1 != net_minute && minute != net_minute && minute+1 != net_minute){
                                int time_result = manager.ZYsetSysTime(timeBean.getDate(),timeBean.getTime());
                                if (time_result == 0){
                                    ToastUtils.showShort("系统时间设置成功");
                                }else {
                                    ToastUtils.showShort("系统时间设置失败");
                                }
                            }
                        }
                    }
                }
            }
        });
    }

    private String[] get_system_time(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd|HH:mm");
        Date date = new Date();
        String format = dateFormat.format(date);
        String[] splits = format.split("\\|");
        return splits;
    }

}
